﻿'*******************************************************************
' Program: Data Center Visio Rack Tool
' Author: Albert E Edlund
' Date:
'
' Purpose:
'
'
'
'*******************************************************************


Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging

Imports System
Imports System.ComponentModel
Imports System.Collections.Generic

Imports System.Collections.ObjectModel


Imports System.Data
Imports System.IO
Imports System.Xml
Imports System.Collections

Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop.Visio



Imports DC_Rack.Support.clsVisioSupport
Imports DC_Rack.Objects.clsDC_RackData
Imports DC_Rack.Objects.dc_Component



Public Class clsAttrApplications


#Region " Properties and definitions "

    Public m_db As Database
    Public m_ds As DataSet
    Public m_dt As DataTable

    Public m_dc As dc_Application
    Public m_oc As Collection(Of dc_Application)

    Private strUserId As String

    Public m_deleteCommand As DbCommand
    Public m_insertCommand As DbCommand
    Public m_updateCommand As DbCommand

    Public _clsVisio As DC_Rack.Support.clsVisioSupport

    ' the attribute tables
    Public Const readAll As String = "selecttblAttrApplication"

    ' who am I
    Private myEnum As DC_RackTables = DC_RackTables.tblAttrApplication

    '********* visio snippet vb.net vis0041vb ******************
    Private _drawingApplication As Microsoft.Office.Interop.Visio.Application
    Public Property drawingApplication() As Microsoft.Office.Interop.Visio.Application
        Get
            Return Me._drawingApplication
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Application)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("ApplicationValueIsNull", _
                    "drawingApplication is Null.")
            End If
            Me._drawingApplication = value
        End Set
    End Property

    '********* visio snippet vb.net vis0043vb ******************
    Private _drawingDocument As Microsoft.Office.Interop.Visio.Document
    Public Property drawingDocument() As Microsoft.Office.Interop.Visio.Document
        Get
            Return Me._drawingDocument
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Document)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("DocumentValueIsNull", _
                    "drawingDocument is Null.")
            End If
            Me._drawingDocument = value
        End Set
    End Property

    '********* visio snippet vb.net vis0045vb ******************
    Private _drawingWindow As Microsoft.Office.Interop.Visio.Window
    Public Property drawingWindow() As Microsoft.Office.Interop.Visio.Window
        Get
            Return Me._drawingWindow
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Window)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("WindowValueIsNull", _
                    "drawingWindow is Null.")
            End If
            Me._drawingWindow = value
        End Set
    End Property

    '********* visio snippet vb.net vis0044vb ******************
    Private _drawingPage As Microsoft.Office.Interop.Visio.Page
    Public Property drawingPage() As Microsoft.Office.Interop.Visio.Page
        Get
            Return Me._drawingPage
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Page)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("PageValueIsNull", _
                    "drawingPage is Null.")
            End If
            Me._drawingPage = value
        End Set
    End Property

    Public m_customstencilpath As String = My.Settings.path_custom_stencils
    Public Property _CustomStencilPath() As String
        Get
            Return Me.m_customstencilpath
        End Get
        Set(ByVal value As String)
            Me.m_customstencilpath = value
        End Set
    End Property



#End Region

#Region " Move/Transform Data "

    '
    ' create columns
    '
    Public Sub CreateTableColumns(ByRef tempTable As DataTable)

        Dim newDC As dc_Application = New dc_Application(strUserId)

        Try


            Dim colappKey As New System.Data.DataColumn
            colappKey.ColumnName = "appKey"
            colappKey.DataType = GetType(Integer)
            colappKey.DefaultValue = newDC.appKey
            tempTable.Columns.Add(colappKey)

            Dim colappOSKey As New System.Data.DataColumn
            colappOSKey.ColumnName = "appOsKey"
            colappOSKey.DataType = GetType(Integer)
            colappOSKey.DefaultValue = newDC.appOSKey
            tempTable.Columns.Add(colappOSKey)

            Dim colappName As New System.Data.DataColumn
            colappName.ColumnName = "appName"
            colappName.DataType = GetType(String)
            colappName.DefaultValue = newDC.appName
            tempTable.Columns.Add(colappName)

            Dim colappVendor As New System.Data.DataColumn
            colappVendor.ColumnName = "appVendor"
            colappVendor.DataType = GetType(String)
            colappVendor.DefaultValue = newDC.appVendor
            tempTable.Columns.Add(colappVendor)

            Dim colappVersion As New System.Data.DataColumn
            colappVersion.ColumnName = "appVersion"
            colappVersion.DataType = GetType(String)
            colappVersion.DefaultValue = newDC.appVersion
            tempTable.Columns.Add(colappVersion)

            Dim colappServicePack As New System.Data.DataColumn
            colappServicePack.ColumnName = "appServicePack"
            colappServicePack.DataType = GetType(String)
            colappServicePack.DefaultValue = newDC.appServicePack
            tempTable.Columns.Add(colappServicePack)

            Dim colappFixLevel As New System.Data.DataColumn
            colappFixLevel.ColumnName = "appFixLevel"
            colappFixLevel.DataType = GetType(String)
            colappFixLevel.DefaultValue = newDC.appFixLevel
            tempTable.Columns.Add(colappFixLevel)

            Dim colappUserCommunity As New System.Data.DataColumn
            colappUserCommunity.ColumnName = "appUserCommunity"
            colappUserCommunity.DataType = GetType(String)
            colappUserCommunity.DefaultValue = newDC.appUserCommunity
            tempTable.Columns.Add(colappUserCommunity)

            Dim colappOwner As New System.Data.DataColumn
            colappOwner.ColumnName = "appOwner"
            colappOwner.DataType = GetType(String)
            colappOwner.DefaultValue = newDC.appOwner
            tempTable.Columns.Add(colappOwner)

            Dim colappMove As New System.Data.DataColumn
            colappMove.ColumnName = "appMove"
            colappMove.DataType = GetType(Boolean)
            colappMove.DefaultValue = newDC.appMove
            tempTable.Columns.Add(colappMove)

            Dim colappOwnerSignoff As New System.Data.DataColumn
            colappOwnerSignoff.ColumnName = "appOwnerSignoff"
            colappOwnerSignoff.DataType = GetType(Boolean)
            colappOwnerSignoff.DefaultValue = newDC.appOwnerSignOff
            tempTable.Columns.Add(colappOwnerSignoff)

            Dim colappOwnerSignoffDate As New System.Data.DataColumn
            colappOwnerSignoffDate.ColumnName = "appOwnerSignoffDate"
            colappOwnerSignoffDate.DataType = GetType(Date)
            colappOwnerSignoffDate.DefaultValue = newDC.appOwnerSignOffDate
            tempTable.Columns.Add(colappOwnerSignoffDate)

            Dim colappSystemOrUser As New System.Data.DataColumn
            colappSystemOrUser.ColumnName = "appSystemOrUser"
            colappSystemOrUser.DataType = GetType(Int16)
            colappSystemOrUser.DefaultValue = newDC.appSystemOrUser
            tempTable.Columns.Add(colappSystemOrUser)

            Dim colappAvailabilityTarget As New System.Data.DataColumn
            colappAvailabilityTarget.ColumnName = "appAvailabilityTarget"
            colappAvailabilityTarget.DataType = GetType(Int16)
            colappAvailabilityTarget.DefaultValue = newDC.appAvailabilityTarget
            tempTable.Columns.Add(colappAvailabilityTarget)

            '
            ' four : critical, high, medium, low
            '
            Dim colappOperationalImpact As New System.Data.DataColumn
            colappOperationalImpact.ColumnName = "appOperationalImpact"
            colappOperationalImpact.DataType = GetType(Int16)
            colappOperationalImpact.DefaultValue = newDC.appOperationalImpact
            tempTable.Columns.Add(colappOperationalImpact)

            Dim colappShape As New System.Data.DataColumn
            colappShape.ColumnName = "appShape"
            colappShape.DataType = GetType(String)
            colappShape.DefaultValue = newDC.appShape
            tempTable.Columns.Add(colappShape)

            Dim colappHoursOfOperations As New System.Data.DataColumn
            colappHoursOfOperations.ColumnName = "appHoursOfOperations"
            colappHoursOfOperations.DataType = GetType(String)
            colappHoursOfOperations.DefaultValue = newDC.appHoursOfOperations
            tempTable.Columns.Add(colappHoursOfOperations)

            Dim colappPeered As New System.Data.DataColumn
            colappPeered.ColumnName = "appPeered"
            colappPeered.DataType = GetType(Boolean)
            colappPeered.DefaultValue = newDC.appPeered
            tempTable.Columns.Add(colappPeered)

            Dim colappIntExt As New System.Data.DataColumn
            colappIntExt.ColumnName = "appIntExt"
            colappIntExt.DataType = GetType(Int16)
            colappIntExt.DefaultValue = newDC.appIntExt
            tempTable.Columns.Add(colappIntExt)

            Dim colappNetworkProtocol As New System.Data.DataColumn
            colappNetworkProtocol.ColumnName = "appNetworkProtocol"
            colappNetworkProtocol.DataType = GetType(String)
            colappNetworkProtocol.DefaultValue = newDC.appNetworkProtocol
            tempTable.Columns.Add(colappNetworkProtocol)

            Dim colappHardCodeIpAddresses As New System.Data.DataColumn
            colappHardCodeIpAddresses.ColumnName = "appHardCodeIpAddresses"
            colappHardCodeIpAddresses.DataType = GetType(String)
            colappHardCodeIpAddresses.DefaultValue = newDC.appHardCodeIpAddresses
            tempTable.Columns.Add(colappHardCodeIpAddresses)

            Dim colappIpPorts As New System.Data.DataColumn
            colappIpPorts.ColumnName = "appIpPorts"
            colappIpPorts.DataType = GetType(String)
            colappIpPorts.DefaultValue = newDC.appIpPorts
            tempTable.Columns.Add(colappIpPorts)

            Dim colappHardCodeMac As New System.Data.DataColumn
            colappHardCodeMac.ColumnName = "appHardCodeMac"
            colappHardCodeMac.DataType = GetType(String)
            colappHardCodeMac.DefaultValue = newDC.appHardCodeMac
            tempTable.Columns.Add(colappHardCodeMac)

            Dim colappComments As New System.Data.DataColumn
            colappComments.ColumnName = "appComments"
            colappComments.DataType = GetType(String)
            colappComments.DefaultValue = newDC.appComments
            tempTable.Columns.Add(colappComments)

            Dim colappLastUpdate As New System.Data.DataColumn
            colappLastUpdate.ColumnName = "appLastUpdate"
            colappLastUpdate.DataType = GetType(Date)
            colappLastUpdate.DefaultValue = newDC.appLastUpdate
            tempTable.Columns.Add(colappLastUpdate)

            Dim colappLastEdit As New System.Data.DataColumn
            colappLastEdit.ColumnName = "appLastEdit"
            colappLastEdit.DataType = GetType(String)
            colappLastEdit.DefaultValue = newDC.appLastEdit
            tempTable.Columns.Add(colappLastEdit)

            newDC = Nothing

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        If newDC IsNot Nothing Then
            newDC = Nothing
        End If

    End Sub

    '
    ' use for writing records
    '
    Public Sub movePropertiesToDataRow _
            (ByRef tempRow As DataRow, _
             ByVal tempDC As dc_Application)

        Try
            With tempDC
                tempRow.SetField("appKey", .appKey)
                tempRow.SetField("appName", .appName)
                tempRow.SetField("appVendor", .appVendor)
                tempRow.SetField("appVersion", .appVersion)
                tempRow.SetField("appServicePack", .appServicePack)
                tempRow.SetField("appFixLevel", .appFixLevel)
                tempRow.SetField("appOsKey", .appOSKey)
                tempRow.SetField("appUserCommunity", .appUserCommunity)
                tempRow.SetField("appOwner", .appOwner)
                tempRow.SetField("appMove", .appMove)
                tempRow.SetField("appOwnerSignoff", .appOwnerSignOff)
                tempRow.SetField("appOwnerSignOffDate", .appOwnerSignOffDate)
                tempRow.SetField("appSystemOrUser", .appSystemOrUser)
                tempRow.SetField("appAvailabilityTarget", .appAvailabilityTarget)
                tempRow.SetField("appOperationalImpact", .appOperationalImpact)
                tempRow.SetField("appShape", .appShape)
                tempRow.SetField("appHoursOfOperations", .appHoursOfOperations)
                tempRow.SetField("appPeered", .appPeered)
                tempRow.SetField("appIntExt", .appIntExt)
                tempRow.SetField("appNetworkProtocol", .appNetworkProtocol)
                tempRow.SetField("appHardCodeIpAddresses", .appHardCodeIpAddresses)
                tempRow.SetField("appIpPorts", .appIpPorts)
                tempRow.SetField("appHardCodeMac", .appHardCodeMac)
                tempRow.SetField("appComments", .appComments)
                tempRow.SetField("appLastUpdate", .appLastUpdate)
                tempRow.SetField("appLastEdit", strUserId)
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
                (ByVal tempRow As DataRow)

        Try

            With m_dc
                .initProperties()
                .appKey = tempRow.Item("appKey")
                .appName = tempRow.Item("appName")
                .appVendor = tempRow.Item("appVendor")
                .appVersion = tempRow.Item("appVersion")
                .appServicePack = tempRow.Item("appServicePack")
                .appFixLevel = tempRow.Item("appFixLevel")
                .appOSKey = tempRow.Item("appOsKey")
                .appUserCommunity = tempRow.Item("appUserCommunity")
                .appOwner = tempRow.Item("appOwner")
                .appMove = tempRow.Item("appMove")
                .appOwnerSignOff = tempRow.Item("appOwnerSignoff")
                .appOwnerSignOffDate = tempRow.Item("appOwnerSignoffDate")
                .appSystemOrUser = tempRow.Item("appSystemOrUser")
                .appAvailabilityTarget = tempRow.Item("appAvailabilityTarget")
                .appOperationalImpact = DirectCast(CInt(tempRow.Field(Of Short)("appOperationalImpact")), dc_Application.appImpact)
                .appShape = tempRow.Item("appShape")
                .appHoursOfOperations = tempRow.Item("appHoursOfOperations")
                .appPeered = tempRow.Item("appPeered")
                .appIntExt = tempRow.Item("appIntExt")
                .appNetworkProtocol = tempRow.Item("appNetworkProtocol")
                .appIpPorts = tempRow.Item("appIpPorts")
                .appHardCodeMac = tempRow.Item("appHardCodeMac")
                .appComments = tempRow.Item("appComments")
                .appLastUpdate = tempRow.Item("appLastUpdate")
                .appLastEdit = tempRow.Item("appLastEdit")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
                (ByVal tempRow As DataRow, _
                 ByRef tempDC As dc_Application)

        Try

            With tempDC
                .initProperties()
                .appKey = tempRow.Item("appKey")
                .appName = tempRow.Item("appName")
                .appVendor = tempRow.Item("appVendor")
                .appVersion = tempRow.Item("appVersion")
                .appServicePack = tempRow.Item("appServicePack")
                .appFixLevel = tempRow.Item("appFixLevel")
                .appOSKey = tempRow.Item("appOsKey")
                .appUserCommunity = tempRow.Item("appUserCommunity")
                .appOwner = tempRow.Item("appOwner")
                .appMove = tempRow.Item("appMove")
                .appOwnerSignOff = tempRow.Item("appOwnerSignoff")
                .appOwnerSignOffDate = tempRow.Item("appOwnerSignoffDate")
                .appSystemOrUser = tempRow.Item("appSystemOrUser")
                .appAvailabilityTarget = tempRow.Item("appAvailabilityTarget")
                .appOperationalImpact = DirectCast(CInt(tempRow.Field(Of Short)("appOperationalImpact")), dc_Application.appImpact)
                .appShape = tempRow.Item("appShape")
                .appHoursOfOperations = tempRow.Item("appHoursOfOperations")
                .appPeered = tempRow.Item("appPeered")
                .appIntExt = tempRow.Item("appIntExt")
                .appNetworkProtocol = tempRow.Item("appNetworkProtocol")
                .appIpPorts = tempRow.Item("appIpPorts")
                .appHardCodeMac = tempRow.Item("appHardCodeMac")
                .appComments = tempRow.Item("appComments")
                .appLastUpdate = tempRow.Item("appLastUpdate")
                .appLastEdit = tempRow.Item("appLastEdit")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Sub moveDataTableToObservableCollection _
                (ByVal dtTemp As DataTable)

        Try

            If 0 < dtTemp.Rows.Count Then

                If m_oc IsNot Nothing Then
                    m_oc = Nothing
                    m_oc = New Collection(Of dc_Application)
                Else
                    m_oc = New Collection(Of dc_Application)
                End If

                Dim drTemp As DataRow
                Dim dcTemp As dc_Application
                For Each drTemp In dtTemp.Rows
                    dcTemp = New dc_Application
                    moveDataRowToProperties(drTemp, dcTemp)
                    m_oc.Add(dcTemp)
                Next

            End If

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub

    '
    '
    '
    Public Function moveDataTableToXML() As XmlDocument

        Dim writer = New System.IO.StringWriter
        Dim m_dt_xml As Xml.XmlDocument = Nothing

        Try


            m_dt.WriteXml(writer, XmlWriteMode.WriteSchema, True)
            m_dt_xml = Nothing
            m_dt_xml = New Xml.XmlDocument
            m_dt_xml.LoadXml(writer.ToString)

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally

            writer = Nothing

        End Try

        Return m_dt_xml

    End Function

#End Region

#Region " Standard Calls "

    '
    ' take the existing Table and put it back into the database
    ' with any changes that may have occured
    '
    Public Function dbUpdateDataSet() As Int16

        Dim rowsAffected As Int16 = 0

        Try

            rowsAffected = m_db.UpdateDataSet(m_ds, dc_racktables.tblAttrApplication.tostring, _
                                        m_insertCommand, _
                                        m_updateCommand, _
                                        m_deleteCommand, _
                                        UpdateBehavior.Standard)
            'msgbox rowsAffected

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If

        End Try

        Return rowsAffected

    End Function

    '
    '
    '
    Private Sub buildInsertApplicationCommand()



        Try
            m_insertCommand = m_db.GetStoredProcCommand("insertAttrApplication")

            Dim cmdActive As DbCommand
            cmdActive = m_insertCommand

            m_db.AddInParameter(cmdActive, "appName", DbType.String, "appName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appVendor", DbType.String, "appVendor", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appVersion", DbType.String, "appVersion", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appServicePack", DbType.String, "appServicePack", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appFixLevel", DbType.String, "appFixLevel", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appOSKey", DbType.Int32, "appOSKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appUserCommunity", DbType.String, "appUserCommunity", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appOwner", DbType.String, "appOwner", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appMove", DbType.Boolean, "appMove", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appOwnerSignoff", DbType.String, "appOwnerSignoff", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appOwnerSignoffDate", DbType.DateTime, "appOwnerSignoffDate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appSystemOrUser", DbType.Int16, "appSystemOrUser", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appAvailabilityTarget", DbType.Int16, "appAvailabilityTarget", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appOperationalImpact", DbType.Int16, "appOperationalImpact", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appShape", DbType.String, "appShape", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appHoursOfOperations", DbType.String, "appHoursOfOperations", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appPeered", DbType.Boolean, "appPeered", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appIntExt", DbType.Int16, "appIntExt", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appNetworkProtocol", DbType.String, "appNetworkProtocol", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appHardCodeIpAddresses", DbType.String, "appHardCodeIpAddresses", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appIPPorts", DbType.String, "appIPPorts", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appHardCodeMac", DbType.String, "appHardCodeMac", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appComments", DbType.String, "appComments", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appLastUpdate", DbType.Date, "appLastUpdate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appLastEdit", DbType.String, "appLastEdit", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub buildDeleteApplicationCommand()

        Try
            m_deleteCommand = m_db.GetStoredProcCommand("deleteAttrApplication")

            Dim cmdActive As DbCommand
            cmdActive = m_deleteCommand

            m_db.AddInParameter(cmdActive, "appKey", DbType.Int32, "appKey", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try
    End Sub

    '
    '
    '
    Private Sub buildUpdateApplicationCommand()


        Try
            m_updateCommand = m_db.GetStoredProcCommand("updateAttrApplication")

            Dim cmdActive As DbCommand
            cmdActive = m_updateCommand

            m_db.AddInParameter(cmdActive, "appKey", DbType.Int32, "appKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appName", DbType.String, "appName", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appVendor", DbType.String, "appVendor", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appVersion", DbType.String, "appVersion", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appServicePack", DbType.String, "appServicePack", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appFixLevel", DbType.String, "appFixLevel", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appOSKey", DbType.Int32, "appOSKey", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appUserCommunity", DbType.String, "appUserCommunity", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appOwner", DbType.String, "appOwner", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appMove", DbType.Boolean, "appMove", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appOwnerSignoff", DbType.String, "appOwnerSignoff", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appOwnerSignoffDate", DbType.DateTime, "appOwnerSignoffDate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appSystemOrUser", DbType.Int16, "appSystemOrUser", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appAvailabilityTarget", DbType.Int16, "appAvailabilityTarget", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appOperationalImpact", DbType.Int16, "appOperationalImpact", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appShape", DbType.String, "appShape", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appHoursOfOperations", DbType.String, "appHoursOfOperations", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appPeered", DbType.Boolean, "appPeered", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appIntExt", DbType.Int16, "appIntExt", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appNetworkProtocol", DbType.String, "appNetworkProtocol", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appHardCodeIpAddresses", DbType.String, "appHardCodeIpAddresses", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appIPPorts", DbType.String, "appIPPorts", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appHardCodeMac", DbType.String, "appHardCodeMac", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appComments", DbType.String, "appComments", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appLastUpdate", DbType.Date, "appLastUpdate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "appLastEdit", DbType.String, "appLastEdit", DataRowVersion.Current)



        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub


    '
    ' create a record based on property values move it to the generic datatable
    ' then copy the table to the database
    '
    Public Function addAttrApplicationDataSourceRecord _
        (ByVal tempDc As dc_Application, _
        Optional ByVal blnSilent As Boolean = False) _
        As Integer

        Dim intRowsAffected As Integer = 0

        Try

            initDataSetAndTable()

            Dim m_dr As DataRow = m_ds.Tables(DC_RackTables.tblAttrApplication.ToString).NewRow
            movePropertiesToDataRow(m_dr, tempDc)
            m_ds.Tables(DC_RackTables.tblAttrApplication.ToString).Rows.Add(m_dr)
            intRowsAffected = dbUpdateDataSet()


            If blnSilent = False Then
                MsgBox("DataRowsUpated = " & intRowsAffected)
            End If

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Return intRowsAffected

    End Function


    '
    '
    '
    Public Sub initDataSetAndTable()

        Try

            ' make sure the root stuff is there
            If m_ds Is Nothing Then
                m_ds = New DataSet
            Else
                m_ds = Nothing
                m_ds = New DataSet
            End If
            m_ds.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema

            If m_dt Is Nothing Then
                m_dt = New DataTable
            Else
                m_dt = Nothing
                m_dt = New DataTable
            End If

            If m_oc Is Nothing Then
                m_oc = New Collection(Of dc_Application)
            Else
                m_oc = Nothing
                m_oc = New Collection(Of dc_Application)
            End If

            If m_dc Is Nothing Then
                m_dc = New dc_Application(strUserId)
            Else
                m_dc = Nothing
                m_dc = New dc_Application(strUserId)
            End If


            CreateTableColumns(m_dt)
            m_dt.TableName = DC_RackTables.tblAttrApplication.ToString
            m_ds.Tables.Add(m_dt)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub




#End Region

#Region " database calls "


    '
    '
    '
    Public Sub dbSelectAllApplications()


        Try

            If m_dc IsNot Nothing Then
                m_dc = Nothing
                m_dc = New dc_Application
            Else
                m_dc = New dc_Application
            End If

            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand(readAll)
            m_db.LoadDataSet(cmd, m_ds, dc_racktables.tblAttrApplication.tostring)

            m_dt = m_ds.Tables(dc_racktables.tblAttrApplication.tostring)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Function dbSelectAttrApplicationViewByCompKey _
            (ByVal intCompKey As Int32) _
            As System.Data.DataTable


        Dim temp_ds As DataSet = New DataSet
        Dim temp_dt As DataTable = New DataTable


        Try

            Dim strViewCompAppls As String = "viewComponentApplications"

            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectAttrApplicationViewByCompKey")
            m_db.AddInParameter(cmd, "@compKey", DbType.Int32, intCompKey)

            m_db.LoadDataSet(cmd, temp_ds, strViewCompAppls)
            temp_dt = temp_ds.Tables(strViewCompAppls)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        dbSelectAttrApplicationViewByCompKey = temp_dt


    End Function

    '
    '
    '
    Public Function dbSelectAttrApplicationsVHViewByCompKey _
            (ByVal intCompKey As Int32) _
            As System.Data.DataTable


        Dim temp_ds As DataSet = New DataSet
        Dim temp_dt As DataTable = New DataTable


        Try

            Dim strViewCompAppls As String = "viewComponentVHApplications"

            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectAttrApplicationVHViewByCompKey")
            m_db.AddInParameter(cmd, "@compKey", DbType.Int32, intCompKey)

            m_db.LoadDataSet(cmd, temp_ds, strViewCompAppls)
            temp_dt = temp_ds.Tables(strViewCompAppls)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        dbSelectAttrApplicationsVHViewByCompKey = temp_dt


    End Function

    '
    '
    '
    Public Sub dbSelectAttrApplicationsBySystemKey(ByVal intSysKey As Int32)


        Try


            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectAttrApplicationBySystemKey")
            m_db.AddInParameter(cmd, "@systemKey", DbType.Int32, intSysKey)
            m_db.LoadDataSet(cmd, m_ds, DC_RackTables.tblAttrApplication.ToString)

            m_dt = m_ds.Tables(DC_RackTables.tblAttrApplication.ToString)

            If m_dt.Rows.Count = 1 Then
                Dim m_dr As System.Data.DataRow = m_dt.Rows(0)
                moveDataRowToProperties(m_dr)
            End If


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' used in the pagefApplication form
    '
    Public Sub dbSelectAttrApplicationsByAppKey(ByVal intAppKey As Int32)


        Try


            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand("selectAttrApplicationByAppKey")
            m_db.AddInParameter(cmd, "@appKey", DbType.Int32, intAppKey)
            m_db.LoadDataSet(cmd, m_ds, DC_RackTables.tblAttrApplication.ToString)

            m_dt = m_ds.Tables(DC_RackTables.tblAttrApplication.ToString)

            If m_dt.Rows.Count = 1 Then
                Dim m_dr As System.Data.DataRow = m_dt.Rows(0)
                moveDataRowToProperties(m_dr)
            End If


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

#End Region

#Region " Constructor "

    '
    '
    '
    Public Sub New(ByVal visApp As Microsoft.Office.Interop.Visio.Application, _
                    ByVal visDoc As Microsoft.Office.Interop.Visio.Document)
        Try


            m_db = DatabaseFactory.CreateDatabase()

            strUserId = Environment.UserDomainName & "/" & Environment.UserName

            initDataSetAndTable()

            _clsVisio = New DC_Rack.Support.clsVisioSupport
            _clsVisio.drawingApplication = visApp
            _clsVisio.drawingDocument = visDoc

            m_updateCommand = Nothing
            m_insertCommand = Nothing
            m_deleteCommand = Nothing

            buildInsertApplicationCommand()
            buildDeleteApplicationCommand()
            buildUpdateApplicationCommand()


        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


#End Region

#Region " Visio Attributes "

    '
    '
    '
    Public Sub addTestAppProperties()

        With m_dc
            .appKey = -1
            .appOSKey = -1
            .appName = "Visio"
            .appVendor = "Microsoft"
            .appVersion = "Professional"
            .appServicePack = "SP2"
            .appOperationalImpact = "low"
        End With

    End Sub

    '
    '
    '
    Public Sub UpdateAttrApplicationProperties _
            (ByRef visShape As Microsoft.Office.Interop.Visio.Shape, _
             ByVal drApp As System.Data.DataRow)


        Try
            Dim intTemp As Integer = -1
            If Not (drApp Is Nothing) Then

                moveDataRowToProperties(drApp)

            Else
                addTestAppProperties()
            End If

            With m_dc
                _clsVisio.ForceShapeData(visShape, "appKey", .appKey, "attrApp")
                _clsVisio.ForceShapeData(visShape, "appOSKey", .appOSKey, "attrApp")
                _clsVisio.ForceShapeData(visShape, "appName", .appName, "attrApp")
                _clsVisio.ForceShapeData(visShape, "appVendor", .appVendor, "attrApp")
                _clsVisio.ForceShapeData(visShape, "appVersion", .appVersion, "attrApp")
                _clsVisio.ForceShapeData(visShape, "appServicePack", .appServicePack, "attrApp")
                _clsVisio.ForceShapeData(visShape, "appOperationalImpact", .appOperationalImpact, "attrApp")


            End With


        Catch com As COMException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    Private Const constAppsPinX As String = "1.5 in"
    Private Const constAppsPinY As String = "5 in"
    Private Const constAppsAlign As VisListAlignment = VisListAlignment.visListAlignLeftOrTop
    Private Const constAppsDir As VisListDirection = VisListDirection.visListDirTopToBottom
    Private Const constAppsHeader As String = "Applications"
    Private Const constAppsMaster As String = "Container 4"

    ' wireframe stencil for list (vertical w/header on side and horizontal) toolbar 
    Const stencilWFToolbar As String = "wftlbr_u.vss"
    Const masterDropDown As String = "Drop-down menu"
    Const masterDropDownItem As String = "Menu item"


    '
    '
    '
    Public Function buildSystemApplicationContainer _
                        (ByVal intSysKey As Int32, _
                       ByVal _clsData As clsDC_RackData) _
                        As Microsoft.Office.Interop.Visio.Shape

        Dim shpContainer As Microsoft.Office.Interop.Visio.Shape = Nothing

        ' just to make sure
        _clsVisio.drawingPage = drawingPage

        Try

  


            Dim colInput As Collection = New Collection
            Dim drTemp As System.Data.DataRow = Nothing
            Dim applicationQuery = From sysapplication In m_ds.Tables(DC_RackTables.tblAttrApplication.ToString).AsEnumerable() _
                       Where sysapplication!apposkey = intSysKey
                      Select sysapplication

            If applicationQuery.Count = 0 Then Return shpContainer

            ' should only be a single operating system 
            For Each a In applicationQuery
                colInput.Add("appKey_" & a.Item("appKey").ToString)
                'System.Diagnostics.Debug.WriteLine(a.Item("appName").ToString)
                'System.Diagnostics.Debug.WriteLine(a.Item("appKey").ToString)
            Next a

            ' create a list control
            Dim listControl As Microsoft.Office.Interop.Visio.Shape = Nothing
            ' pass in the created collection and return a populated list control
            Dim colMembers As Collection = New Collection
            colMembers = _clsVisio.dropDropDownBoxItems(colInput, masterDropDownItem, stencilWFToolbar, "1.5 in", ".25 in", vbTrue)

            Dim visShape As Microsoft.Office.Interop.Visio.Shape = Nothing
            Dim visCell As Microsoft.Office.Interop.Visio.Cell = Nothing
            Dim strName As String = ""
            Dim strKey As String = ""
            For Each shapeId In colMembers
                ' get the shape
                visShape = drawingPage.Shapes.ItemFromID(shapeId)

                If visShape.CellExists("user.attrkey", False) = True Then
                    strName = visShape.Cells("user.attrkey").ResultStr("")
                Else
                    strName = ""
                    GoTo NextShape
                End If

                ' go read the current records
                For Each a In applicationQuery
                    strKey = "appKey_" & a.Item("appKey").ToString
                    If strName = strKey Then
                        UpdateAttrApplicationProperties(visShape, a)
                        visShape.Text = a.Item("appName").ToString

                        ' move the name to comment field so we get mouse over
                        If visShape.CellsSRCExists(VisSectionIndices.visSectionObject, VisRowIndices.visRowMisc, VisCellIndices.visComment, False) = True Then
                            visCell = visShape.CellsSRC(VisSectionIndices.visSectionObject, VisRowIndices.visRowMisc, VisCellIndices.visComment)
                            _clsVisio.SetCellValueToString(visCell, strName)
                        End If

                        Exit For
                    End If

                Next a
NextShape:
            Next shapeId

            shpContainer = _clsVisio.addContainerToPage(constAppsMaster, "contApplications", "")

            listControl = _clsVisio.loadListControl(colMembers, constAppsAlign, constAppsDir)
            listControl.Name = "listApps"
            listControl.NameU = "listApps"

            ' add the list control to the container
            _clsVisio.AddShapeToContainer(shpContainer, listControl)

            shpContainer.Cells("pinx").Formula = constAppsPinX
            shpContainer.Cells("piny").Formula = constAppsPinY



        Catch com As COMException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        System.Windows.Forms.Application.DoEvents()
        Return shpContainer

    End Function




#End Region




End Class
